package com.ss.android.download;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

final class r {
    final File a;
    final File b;
    final Context c;
    private final File d;
    private int e;
    private int f;
    private Thread g;

    public r(Context arg2) {
        this.e = 0;
        this.f = 0;
        this.g = null;
        this.c = arg2;
        this.b = arg2.getCacheDir();
        this.a = Environment.getExternalStorageDirectory();
        this.d = Environment.getDownloadCacheDirectory();
        this.c();
    }

    final synchronized void a() {
        int v0_1 = this.f + 1;
        this.f = v0_1;
        if (v0_1 % 250 == 0) {
            this.c();
        }
    }

    final void a(int arg6, String arg7, long arg8) throws StopRequestException {
        this.d();
        File v0 = null;
        if (com.ss.android.download.aa.c) {
            Log.i("SsDownloadManager", "in verifySpace, destination: " + arg6 + ", path: " + arg7 + ", length: " + arg8);
        }

        if (arg7 == null) {
            throw new IllegalArgumentException("path can\'t be null");
        }

        switch (arg6) {
            case 0: {
                v0 = this.a;
                break;
            }
            case 1: {
                if (arg7.startsWith(this.a.getPath())) {
                    v0 = this.a;
                    break;
                }

                if (arg7.startsWith(this.b.getPath())) {
                    v0 = this.b;
                   break;
                }

                if (!arg7.startsWith(this.d.getPath())) {
                    break;
                }

                v0 = this.d;
                break;
            }
        }

        label_23:
        if (v0 == null) {
            throw new IllegalStateException("invalid combination of destination: " + arg6 + ", path: " + arg7);
        }

        this.a(v0, arg8, arg6);
    }

    private long a(int arg11) {
        long v0 = 0;
        long v6 = 0;
        if (com.ss.android.download.aa.c) {
            Log.i("SsDownloadManager", "discardPurgeableFiles: destination = " + arg11 + ", targetBytes = 10485760");
        }

        Cursor v2 = k.a(this.c).a(com.ss.android.download.a.a.a, null, "( status = \'200\' AND destination = ? )", new String[]{String.valueOf(arg11)}, "lastmod");
        if (v2 == null) {
            v0 = v6;
            return v0;
        }

        try {
            int v3 = v2.getColumnIndex("_data");
            v0 = v6;
            while (v2.moveToNext()) {
                if (v0 >= 10485760) {
                    break;
                }

                String v4 = v2.getString(v3);
                if (TextUtils.isEmpty(((CharSequence) v4))) {
                    continue;
                }

                File v5 = new File(v4);
                if (com.ss.android.download.aa.c) {
                    Log.d("SsDownloadManager", "purging " + v5.getAbsolutePath() + " for " + v5.length() + " bytes");
                }

                v0 += v5.length();
                v5.delete();
                k.a(this.c).a(ContentUris.withAppendedId(com.ss.android.download.a.a.a, v2.getLong(v2.getColumnIndex("_id"))));
            }
        } catch (Throwable v0_1) {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception v1) {
                }
            }
        }

        if (v2 != null) {
            try {
                v2.close();
            } catch (Exception v2_1) {
            }
        }

        if (com.ss.android.download.aa.c) {
            Log.i("SsDownloadManager", "Purged files, freed " + v0 + " for 10485760 requested");
        }

        return v0;
    }

    private static long a(File arg7) {
        long v0 = 104857600;
        File[] v3 = arg7.listFiles();
        if (v3 != null) {
            int v6 = v3.length;
            int v2 = 0;
            while (v2 < v6) {
                long v4 = v0 - v3[v2].length();
                ++v2;
                v0 = v4;
            }

            if (com.ss.android.download.aa.c) {
                Log.i("SsDownloadManager", "available space (in bytes) in downloads data dir: " + v0);
            }
        }

        return v0;
    }

    private synchronized void a(File arg9, long arg10, int arg12) throws StopRequestException {
        long v6 = 10485760;
        if (arg10 != 0) {
            if (arg12 == 1 || arg12 == 0) {
                if (!Environment.getExternalStorageState().equals("mounted")) {
                    throw new StopRequestException(199, "external media not mounted");
                }
            }
            long v0_1 = r.b(arg9);
            if (v0_1 < v6) {
                this.a(arg12);
                this.b();
                v0_1 = r.b(arg9);
                if (v0_1 < v6) {
                    if (arg9.equals(this.d)) {
                        Log.w("SsDownloadManager", "System cache dir (\'/cache\') is running low on space.space available (in bytes): " + v0_1);
                    } else {
                        throw new StopRequestException(198, "space in the filesystem rooted at: " + arg9 + " is below 10% availability. stopping this download.");
                    }
                }
            }

            if (arg9.equals(this.b)) {
                v0_1 = r.a(this.b);
                if (v0_1 < v6) {
                    Log.w("SsDownloadManager", "Downloads data dir: " + arg9 + " is running low on space. space available (in bytes): " + v0_1);
                }

                if (v0_1 >= arg10) {

                } else {
                    this.a(arg12);
                    this.b();
                    v0_1 = r.a(this.b);
                }
            }
            if (v0_1 >= arg10) {
                return;
            }
            throw new StopRequestException(198, "not enough free space in the filesystem rooted at: " + arg9 + " and unable to free any more");
        }
    }

    final synchronized int a(long arg4) {
        this.e = ((int) ((((long) this.e)) + arg4));
        return this.e;
    }

    private static long b(File arg6) {
        StatFs v0 = new StatFs(arg6.getPath());
        long v0_1 = (((long) v0.getBlockSize())) * ((((long) v0.getAvailableBlocks())) - 4);
        if (com.ss.android.download.aa.c) {
            Log.i("SsDownloadManager", "available space (in bytes) in filesystem rooted at: " + arg6.getPath() + " is: " + v0_1);
        }

        return v0_1;
    }

    final void b() {
        Cursor v1 = null;
        ArrayList v6;
        if (com.ss.android.download.aa.c) {
            Log.i("SsDownloadManager", "in removeSpuriousFiles");
        }

        try {
            v6 = new ArrayList();
            File[] v0_1 = this.d.listFiles();
            if (v0_1 != null) {
                ((List) v6).addAll(Arrays.asList(((Object[]) v0_1)));
            }

            v0_1 = this.b.listFiles();
            if (v0_1 != null) {
                ((List) v6).addAll(Arrays.asList(((Object[]) v0_1)));
            }

            if (((List) v6).size() == 0) {
                return;
            }

            v1 = k.a(this.c).a(com.ss.android.download.a.a.a, new String[]{"_data"}, null, null, null);
            if (v1 != null) {
                while (v1.moveToNext()) {
                    String v0_3 = v1.getString(0);
                    if (TextUtils.isEmpty(((CharSequence) v0_3))) {
                        continue;
                    }

                    if (com.ss.android.download.aa.c) {
                        Log.i("SsDownloadManager", "in removeSpuriousFiles, preserving file " + v0_3);
                    }

                    ((List) v6).remove(new File(v0_3));
                }
            }
        } catch (Exception v0) {
            v0.printStackTrace();
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception v0) {
                }
            }
        }
    }

    private synchronized void c() {
        if (this.g == null || !this.g.isAlive()) {
            this.g = new s(this);
            this.g.start();
        }
    }

    private synchronized void d() {
        this.e = 0;
    }
}

